package com.inforich.web.controller.disposal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.core.domain.entity.SysUser;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.SecurityUtils;
import com.inforich.common.utils.poi.ExcelUtil;
import com.inforich.disposal.domain.DisposalLandInfo;
import com.inforich.disposal.service.IDisposalLandInfoService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 资源化企业再生产品月度信息汇总Controller
 *
 * @author hjm
 * @date 2023-07-05
 */
@RestController
@RequestMapping("/disposal/disposalLandInfo")
public class DisposalLandInfoController extends BaseController {

  @Autowired
  private IDisposalLandInfoService disposalLandInfoService;

  /**
   * 查询资源化企业再生产品月度信息汇总列表
   */
  @PreAuthorize("@ss.hasPermi('disposal:disposalLandInfo:list')")
  @GetMapping("/list")
  public Page<DisposalLandInfo> list(Page<DisposalLandInfo> page,
      DisposalLandInfo disposalLandInfo) {
    SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    Long disposalLandId = sysUser.getDisposalLandId();
    if (disposalLandId == null) {
      return page;
    }
    disposalLandInfo.setDisposalLandId(disposalLandId);
    Map<String, Object> map = beanToMap(disposalLandInfo);
    disposalLandInfoService.pageDisposalLandInfo(page, map);
    return page;
  }

  /**
   * 导出资源化企业再生产品月度信息汇总列表
   */
  @PreAuthorize("@ss.hasPermi('disposal:disposalLandInfo:export')")
  @Log(title = "资源化企业再生产品月度信息汇总", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, DisposalLandInfo disposalLandInfo) {
    SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    Long disposalLandId = sysUser.getDisposalLandId();
    if (disposalLandId == null) {
      ExcelUtil<DisposalLandInfo> util = new ExcelUtil<DisposalLandInfo>(DisposalLandInfo.class);
      util.exportExcel(response, new ArrayList<>(), "资源化企业再生产品月度信息汇总数据");
    } else {
      disposalLandInfo.setDisposalLandId(disposalLandId);
      Map<String, Object> map = beanToMap(disposalLandInfo);
      List<DisposalLandInfo> list = disposalLandInfoService.listDisposalLandInfo(map);
      ExcelUtil<DisposalLandInfo> util = new ExcelUtil<DisposalLandInfo>(DisposalLandInfo.class);
      util.exportExcel(response, list, "资源化企业再生产品月度信息汇总数据");
    }

  }

  /**
   * 获取资源化企业再生产品月度信息汇总详细信息
   */
  @PreAuthorize("@ss.hasPermi('disposal:disposalLandInfo:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return AjaxResult.success(disposalLandInfoService.getById(id));
  }

  /**
   * 新增资源化企业再生产品月度信息汇总
   */
  @PreAuthorize("@ss.hasPermi('disposal:disposalLandInfo:add')")
  @Log(title = "资源化企业再生产品月度信息汇总", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody DisposalLandInfo disposalLandInfo) {
    SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    Long disposalLandId = sysUser.getDisposalLandId();
    if (disposalLandId == null) {
      return AjaxResult.error("您不是资源化企业管理员，请联系管理员处理");
    }
    disposalLandInfo.setDisposalLandId(disposalLandId);
    int count = disposalLandInfoService.count(new LambdaQueryWrapper<DisposalLandInfo>()
        .eq(DisposalLandInfo::getDisposalLandId, disposalLandInfo.getDisposalLandId())
        .eq(DisposalLandInfo::getMonth, disposalLandInfo.getMonth())
        .eq(DisposalLandInfo::getSysRubbishType, disposalLandInfo.getSysRubbishType())
        .eq(DisposalLandInfo::getSysRubbishTypeSub, disposalLandInfo.getSysRubbishTypeSub()));
    if (count > 0) {
      return AjaxResult.error("不可重复上传，请检查数据");
    }
    disposalLandInfo.setCreateBy(String.valueOf(sysUser.getUserId()));
    disposalLandInfo.setCreateName(sysUser.getNickName());
    return toAjax(disposalLandInfoService.save(disposalLandInfo));
  }

  /**
   * 修改资源化企业再生产品月度信息汇总
   */
  @PreAuthorize("@ss.hasPermi('disposal:disposalLandInfo:edit')")
  @Log(title = "资源化企业再生产品月度信息汇总", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody DisposalLandInfo disposalLandInfo) {
    return toAjax(disposalLandInfoService.updateById(disposalLandInfo));
  }

  /**
   * 删除资源化企业再生产品月度信息汇总
   */
  @PreAuthorize("@ss.hasPermi('disposal:disposalLandInfo:remove')")
  @Log(title = "资源化企业再生产品月度信息汇总", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(disposalLandInfoService.removeByIds(Arrays.asList(ids)));
  }
}
